package com.turing.data.controller;

import com.turing.data.dao.review.ReviewContentMapper;
import com.turing.data.inner.FileTaskInnerService;
import com.turing.data.inner.TaskRecordInnerService;
import com.turing.data.service.element.ElementService;
import com.turing.data.service.review.ReviewService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("Output")
public class OutputController {

    @Value("${fdfs.domain}")
    private String MEDIA_PATH;

    @Value("${answer.path}")
    private String ANSWER_PATH;

    @Autowired
    private ReviewService reviewService;

    @Autowired
    private FileTaskInnerService fileTaskInnerService;

    @Autowired
    private TaskRecordInnerService taskRecordInnerService;

    @Autowired
    private ReviewContentMapper reviewContentMapper;

    private Log logger = LogFactory.getLog(OutputController.class);

    @Autowired
    private ElementService elementService;

    /*@GetMapping("outputAnalyzeFile")
    public void outputAnalyzeFile(String templateId, String path) {
        List<Element> elementNames = elementService.getElementsByTemplateSn(templateId);
        XSSFWorkbook workbook = new XSSFWorkbook();

        List<Integer> courseIds = elementNames.stream().map(Element::getId).collect(Collectors.toList());
        Map<Integer, String> map = elementNames.stream().collect(Collectors.toMap(Element::getId, Element::getName));
        if (!CollectionUtils.isEmpty(courseIds)) {
            logger.info("要素数量:+++++++++++" + courseIds);
            for (Integer eleId : courseIds) {
                String eleName = map.get(eleId);
                String name = eleName.replaceAll("/", "");
                XSSFSheet sheet = workbook.createSheet(name);
                List<ReviewContent> reviewContentList = new LambdaQueryChainWrapper<>(reviewContentMapper)
                        .eq(ReviewContent::getEleId, eleId)
                        .list();
                logger.info(eleName + "要素de数量:+++++++++++" + reviewContentList.size());
                if (!CollectionUtils.isEmpty(reviewContentList)) {
                    XSSFRow row0 = sheet.createRow(0);
                    row0.createCell(0).setCellValue("任务Id");
                    row0.createCell(1).setCellValue("元素Id");
                    row0.createCell(2).setCellValue("要素名");
                    row0.createCell(3).setCellValue("抽取结果json");
                    row0.createCell(4).setCellValue("抽取文本");
                    row0.createCell(5).setCellValue("修改文本");
                    row0.createCell(6).setCellValue("要素类型");
                    row0.createCell(7).setCellValue("创建时间");

                    int r = 1;
                    for (int ij = 0; ij < reviewContentList.size(); ij++) {
                        ReviewContent reviewContent = reviewContentList.get(ij);

                        String taskSn = reviewContent.getTaskSn();
                        Date createdTime = reviewContent.getCreatedTime();
                        String content = reviewContent.getContent();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        XSSFRow row = sheet.createRow(r);
                        row.createCell(0).setCellValue(taskSn);
                        row.createCell(1).setCellValue(eleId);
                        row.createCell(2).setCellValue(eleName);
                        //row.createCell(3).setCellValue(content);
                        //row.createCell(6).setCellValue(version);
                        row.createCell(7).setCellValue(simpleDateFormat.format(createdTime));

                        JSONArray jsonArray = JSONArray.parseArray(content);

                        int size = jsonArray.size();
                        for (int j = 0; j < size; j++) {
                            if (j == 0) {
                                Object o = jsonArray.get(j);
                                JSONObject jSONObject = JSONObject.parseObject(o.toString());
                                String text = (String) jSONObject.get("text");
                                String businessValue = (String) jSONObject.get("businessValue");
                                if (text != null) {
                                    row.createCell(4).setCellValue(text);
                                }
                                if (businessValue != null) {
                                    row.createCell(5).setCellValue(businessValue);
                                }
                            } else {
                                XSSFRow roww = sheet.createRow(r + ij + j);
                                Object o = jsonArray.get(j);
                                JSONObject jSONObject = JSONObject.parseObject(o.toString());
                                String text = (String) jSONObject.get("text");
                                String businessValue = (String) jSONObject.get("businessValue");
                                if (text != null) {
                                    roww.createCell(4).setCellValue(text);
                                }
                                if (businessValue != null) {
                                    roww.createCell(5).setCellValue(businessValue);
                                }

                            }
                        }
                        r = r + size + 1;
                    }
                }
            }
        }

        try {
            File file = new File(path + templateId + UUID.randomUUID() + ".xlsx");
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ExcelUtils.outputExcelByWorkbook(workbook, fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/


}
